package com.ship.auth.service;

import com.ship.common.core.utils.HttpUtils;
import com.ship.common.core.utils.ServletUtils;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.system.api.domain.SysLoginRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ship.common.core.constant.Constants;
import com.ship.common.core.constant.SecurityConstants;
import com.ship.common.core.utils.StringUtils;
import com.ship.common.core.utils.ip.IpUtils;
import com.ship.system.api.RemoteLogService;
import com.ship.system.api.domain.SysLogininfor;

import java.util.Date;

/**
 * 记录日志方法
 * 
 * @author zhangyh
 */
@Component
public class SysRecordLogService
{
    @Autowired
    private RemoteLogService remoteLogService;

    /**
     * 记录登录信息
     * 
     * @param username 用户名
     * @param status 状态
     * @param message 消息内容
     * @return
     */
    public void recordLogininfor(String username, String status, String message)
    {
//        SysLogininfor logininfor = new SysLogininfor();
//        logininfor.setUserName(username);
//        logininfor.setIpaddr(IpUtils.getIpAddr());
//        logininfor.setMsg(message);
//        // 日志状态
//        if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
//        {
//            logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
//        }
//        else if (Constants.LOGIN_FAIL.equals(status))
//        {
//            logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
//        }
        SysLoginRecord loginRecord = new SysLoginRecord();
        String ip = IpUtils.getIpAddr();
        loginRecord.setIp(ip);
        loginRecord.setCreateTime(new Date());
        loginRecord.setCreateDate(loginRecord.getCreateTime());
        loginRecord.setUserName(username);
        loginRecord.setCreateBy(username);
        loginRecord.setOs(HttpUtils.getOsName(ServletUtils.getRequest()));
        loginRecord.setDevice(HttpUtils.getOsName(ServletUtils.getRequest()));
        loginRecord.setBrowser(HttpUtils.getBrowserName(ServletUtils.getRequest()));
        if (Constants.LOGIN_FAIL.equals(status)){
            loginRecord.setLoginType(2);
        }else if(Constants.LOGIN_SUCCESS.equals(status)){
            loginRecord.setLoginType(1);
        }

        remoteLogService.saveLogininfor(loginRecord);
    }
}
